package com.hdjk.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.activity.financial.FundDetailActivity;
import com.hdjk.view.entity.ShopCarEntity;
import com.hdjk.view.util.StringAppUtil;

import java.util.List;

import static com.hdjk.view.R.id.et_money;
import static com.hdjk.view.R.id.iv_check;

/**
 * Created by sxy on 2017/11/2.
 */

public class ShopCarListAdapter extends BaseAdapter {
    private List<ShopCarEntity> mList;
    private Context mContext;
    private OnMyItemClick mMainClick;

    public ShopCarListAdapter(Context context,OnMyItemClick mainClick) {
        mContext = context;
        mMainClick=mainClick;
    }

    public void setDataList(List<ShopCarEntity> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public ShopCarEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shop_car_list, null);
            holder.rel_main= (RelativeLayout) convertView.findViewById(R.id.rel_main);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
            holder.iv_check = (ImageView) convertView.findViewById(iv_check);
            holder.et_money = (EditText) convertView.findViewById(et_money);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        final ShopCarEntity entity = mList.get(position);
        holder.tv_name.setText(entity.getFund().getFundName());
        holder.tv_code.setText(entity.getFund().getFundCode());
        holder.tv_type.setText(entity.getFund().getFundTypeName());
        holder.et_money.setText(StringAppUtil.isEmptyOrNull(entity.getAmount())?"":entity.getAmount());
        if (entity.getShopCarSelect()){
            holder.iv_check.setImageDrawable(mContext.getResources().getDrawable(R.drawable.gwc_xuanzhong_sel));
        }else {
            holder.iv_check.setImageDrawable(mContext.getResources().getDrawable(R.drawable.gwc_xuanzhong_dis));
        }
        final View finalConvertView = convertView;
        holder.rel_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, FundDetailActivity.class);
                intent.putExtra("id",entity.getFundCode());
                mContext.startActivity(intent);
            }
        });
        holder.iv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMainClick.onDetailClick(finalConvertView,position);
            }
        });
        holder.et_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mMainClick.onEditTextFinish(finalConvertView,position);
            }
        });
        return convertView;
    }

    class Holder {
        RelativeLayout rel_main;
         ImageView iv_check;
         TextView tv_name;
         TextView tv_code;
         TextView tv_type;
         EditText et_money;
    }

    public interface OnMyItemClick{
        void onDetailClick(View view,int position);
        void onEditTextFinish(View view,int position);
    }
}